<script setup>
import { ref,onMounted,watch ,onUnmounted} from 'vue';
import request from '@/utils/request.js'
import { useUserStore } from '@/stores/index.js'
import { useRouter } from 'vue-router'
import { debounce } from 'lodash'; // 导入防抖函数
import {  ElMessage  } from 'element-plus';
const router = useRouter()

const userStore = useUserStore();
// 匹配排序方式
const matchSort = ref('matchDesc');
// 伙伴列表数据
const partnerList = ref([]);

// 查看详情方法
const viewDetail = (partnerStudentId,partnerTotalScore) => {
  // 跳转到FindPartnerContent页面，并传递伙伴学号和匹配度
  router.push(`/partner/FindPartnerInfo?studentId=${partnerStudentId}&partnerTotalScore=${partnerTotalScore}`)

};
// 发送邀请方法
const sendInvitation = async (partnerUserId) => {
  // 可添加发送邀请的逻辑，如调用后端接口
  //跳出邀请框，输入邀请理由
  const inviteMsg = prompt("请输入邀请理由：");
  if (inviteMsg) {
    // 若用户提供了理由，则发送请求给后端
     const res = await request.post('/sendPartnerInvite',{
      receiverId:partnerUserId,
      senderId:userStore.userId,
      inviteMsg: inviteMsg
    } )
    if(res.status === 200){
      ElMessage.success('邀请已发送！');
    }
  } else {
    ElMessage.error('邀请取消！');
  }

};
// 匹配伙伴方法
const matchPartners =async () => {
  const res =await request.get('/matchPartners',
            {
              params: {
                userId: userStore.userId,
                sortType: matchSort.value,
              },
            }
          )
          //将结果赋值给partnerList1.data
 partnerList.value = res.data
};
// 创建防抖版本的匹配方法（300ms延迟：短时间内多次调用只执行最后一次）
const debouncedMatchPartners = debounce(matchPartners, 300);
//监听matchSort的变化，重新匹配伙伴
watch(matchSort, (newVal) => {
  debouncedMatchPartners()
});
// 组件卸载时清理防抖（避免内存泄漏）
onUnmounted(() => {
  debouncedMatchPartners.cancel(); // 取消未执行的防抖任务
});
onMounted(()=>{
  debouncedMatchPartners();
})
</script>
<template>
  <div class="find-partner-page">
    <div class="find-partner-page-head">
      <h1>找学习伙伴</h1>
      <!-- 筛选与匹配区域 -->
      <div class="filter-match-bar">
        <div class="filter-group">
          <label class="filter-label">排序：</label>
          <select v-model="matchSort" class="sort-select">
            <option value="matchDesc">匹配度从高到低</option>
            <option value="courseDesc">共同课程数量从多到少</option>
          </select>
        </div>
      </div>

    </div>
    <!-- 伙伴列表区域 -->
    <div class="partner-grid">
      <div
        v-for="(partner, index) in partnerList"
        :key="index"
        class="partner-card"
      >
        <div class="card-header">
          <img :src="partner.avatar" alt="partner avatar" class="avatar" />
          <div class="name-info">
            <h3>{{ partner.nickname }}</h3>
            <p>{{ partner.studentId }} | {{ partner.major }} {{ partner.grade }}</p>
          </div>
        </div>
        <div class="match-info">
          <p class="rate-label">{{ partner.rateLabel }}</p>
          <div class="progress-container">
            <div class="progress-bar">
              <div
                class="progress"
                :style="{ width: partner.totalScore + '%' }"
              ></div>
            </div>
            <span class="rate-text">{{ partner.totalScore }}%</span>
          </div>
          <p class="common-courses">
           <span class="common-courses-count"> {{ partner.commonCoursesCount}}门共同课程：</span>
            <br>
            {{ partner.commonCourses.join('、') }}
          </p>
        </div>
        <div class="card-actions">

          <button
            class="detail-btn"
            @click="viewDetail(partner.studentId,partner.totalScore)"
          >查看详情</button>
          <button
            class="invite-btn"
            @click="sendInvitation(partner.userId)"
          >发送邀请</button>
        </div>
      </div>
    </div>
  </div>
</template>



<style scoped>
.find-partner-page {
  max-width: 1400px;
  margin: 0 auto;
  padding:0 30px 30px 30px;
  background-color: #f5f9ff; /* 页面背景浅蓝 */
}

.find-partner-page-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  height: 80px;
  width: 100%;
  background-color: #fff;
}

h1 {
  color: #27ae60; /* 松绿色主色 */
  font-size: 40px;
  margin-bottom: 30px;
  margin-left: 50px;
  font-weight: 600;
  font-family: 'Georgia', serif;
  letter-spacing: 1px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.05); /* 轻阴影 */
  transition: all 0.3s ease;
  position: relative;
}

h1:hover {
  color: #264653; /* 深海蓝悬停色 */
  transform: translateY(-2px);
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
}
/* 主容器 */
.filter-match-bar {
  display: flex;
  justify-content: space-between;
  margin: 20px auto;
  padding: 12px 20px;

  max-width: 1200px;
  background-color: #f8fafc;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(52, 152, 219, 0.08);
}

/* 筛选组 */
.filter-group {
  display: flex;
  align-items: center;
  gap: 15px; /* 使用gap替代margin */
}

/* 标签文字 */
.filter-label {
  font-size: 28px;
  color:#27ae60 ;
  font-weight: 500;
  white-space: nowrap; /* 防止文字换行 */
}

/* 下拉框基础样式 */
.sort-select {
  min-width: 150px;
  max-width: 250px;
  height: 40px;
  padding: 0 15px;
  line-height: 40px;
  border: 1px solid #bdc3c7;
  border-radius: 6px;
  background-color: #f8f9fa;
  color: #2c3e50;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}
/* 悬停状态 */
.sort-select:hover {
  border-color: #3498db;
  background-color: #e8f4fc;
  box-shadow: 0 2px 5px rgba(52, 152, 219, 0.15);
}

/* 激活状态 */
.sort-select:focus {
  outline: none;
  border-color: #2980b9;
  box-shadow: 0 0 0 2px rgba(41, 128, 185, 0.2);
}
/* 移动端基础样式 */
@media (max-width: 768px) {
  .filter-match-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .sort-select {
    min-width: 120px;
    height: 36px;
    line-height: 36px;
  }
}
.partner-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 30px;
}

.partner-card {
  border: 1px solid #e6f2ff; /* 卡片边框浅蓝 */
  border-radius: 12px;
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s, box-shadow 0.3s;
}

.partner-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 16px rgba(0, 123, 255, 0.1);
}

.card-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}
.common-courses-count{
  font-size: 16px;
  color:#27ae60;
  margin-bottom: 10px;
}
.avatar {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  margin-right: 15px;
  border: 2px solid #e6f2ff;
}

.name-info h3 {
  font-size: 20px;
  color: #000;
  margin-bottom: 5px;
}

.name-info p {
  font-size: 14px;
  color: #666;
}

.match-info {
  margin-bottom: 15px;
}

.rate-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 6px;
  display: block;
}

.progress-container {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.progress-bar {
  flex: 1;
  height: 8px;
  background-color: #e9ecef;
  border-radius: 4px;
  overflow: hidden;
  margin-right: 10px;
}

.progress {
  height: 100%;
  background-color: #27ae60; /* 进度条主蓝 */
  border-radius: 4px;
  transition: width 0.5s;
}

.rate-text {
  font-size: 14px;
  color: #27ae60;
  font-weight: 600;
}

.common-courses {
  font-size: 13px;
  color: #e74c3c; /* 共同课程文字红 */
  line-height: 1.5;
}

.card-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
}

.collect-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: #999;
  padding: 6px;
  border-radius: 50%;
  transition: background-color 0.3s, color 0.3s;
}

.collect-btn:hover {
  background-color: #f5f5f5;
}

.collect-btn.collected {
  color: #e74c3c; /* 已收藏变红 */
}

.icon {
  width: 22px;
  height: 22px;
}

.detail-btn,
.invite-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.detail-btn {
  background-color: #27ae60; /* 查看详情按钮主蓝 */
  color: #fff;
}

.detail-btn:hover {
  background-color: #0056b3; /* 查看详情按钮 hover 深蓝 */
  transform: translateY(-2px);
}

.invite-btn {
  background-color: #27ae60; /* 发送邀请按钮主蓝 */
  color: #fff;
}

.invite-btn:hover {
  background-color: #0056b3; /* 发送邀请按钮 hover 深蓝 */
  transform: translateY(-2px);
}
</style>
